package com.example.deliveryapp;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.fragment.app.Fragment;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

public class MainPageFragment extends Fragment {
    private RecyclerView restaurantRecyclerView;
    private RestaurantAdapter restaurantAdapter;
    private String userPhone;
    private TextView PhoneNumber;
    private CircleImageView account_mainpage;

    public MainPageFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_mainpage, container, false);

        restaurantRecyclerView = view.findViewById(R.id.restaurant_recycler_view);

        // 初始化店铺数据
        List<Restaurant> restaurants = new ArrayList<>();
        restaurants.add(new Restaurant("Cactus Club Cafe", "30 - 32 mins | Free Delivery on order above $15", 4.3f, R.drawable.sushi_mainpage, R.drawable.store_logo_1_mainpage));
        restaurants.add(new Restaurant("The Fin Liquor Store", "30 - 32 mins | Free Delivery on order above $15", 4.0f, R.drawable.store_alchohol_mainpage, R.drawable.cover_the_fin_liquor_store));
        restaurants.add(new Restaurant("7 - Eleven", "30 - 32 mins | Free Delivery on order above $15", 4.3f, R.drawable.store_7elevent_mainpage, R.drawable.cover_711));
        // ... 添加更多店铺

        restaurantAdapter = new RestaurantAdapter(getContext(), restaurants);
        restaurantRecyclerView.setAdapter(restaurantAdapter);
        restaurantRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

        restaurantAdapter.setOnItemClickListener(new RestaurantAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                // 获取点击的店铺数据
                Restaurant selectedRestaurant = restaurants.get(position);

                // 创建 Intent 来启动 RestaurantMenuPageActivity
                Intent intent = new Intent(getContext(), RestaurantMenuPageActivity.class);
                // 可以通过 putExtra 方法传递需要的数据，例如店铺的 ID 或者名称
                intent.putExtra("restaurant_name", selectedRestaurant.getName());
                intent.putExtra("restaurant_rating", selectedRestaurant.getRating());
                intent.putExtra("restaurant_cover_image", selectedRestaurant.getCoverImageResourceId());

                // 启动新的 Activity
                startActivity(intent);
            }
        });

        // 初始化手机号文本视图
        PhoneNumber = view.findViewById(R.id.phone_number); // 确保这是正确的 ID

        // 从 SharedPreferences 获取手机号
        SharedPreferences sharedPreferences = getActivity().getSharedPreferences("UserPrefs", Context.MODE_PRIVATE);
        // 将默认值改为 "No login" 而不是 null
        String userPhone = sharedPreferences.getString("userPhone", "No login");
        boolean isLoggedIn = sharedPreferences.getBoolean("isLoggedIn", false);

        // 如果用户未登录，确保userPhone设置为"No login"
        if (!isLoggedIn) {
            userPhone = "No login";
        }

        // 根据登录状态设置手机号文本
        if (isLoggedIn) {
            PhoneNumber.setText(userPhone); // 用户已登录，显示手机号
        } else {
            PhoneNumber.setText("No login"); // 用户未登录，显示"No login"
        }


        // 手机号点击事件
        PhoneNumber.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isLoggedIn) {
                    // 如果已登录，则跳转到个人主页
                    ((MainPageActivity) getActivity()).showMyAccountPageFragment();
                } else {
                    // 如果未登录，则跳转到登录界面
                    Intent intent = new Intent(getActivity(), SignInActivity.class);
                    startActivity(intent);
                }
            }
        });
        //初始化头像视图
        account_mainpage = view.findViewById(R.id.account_mainpage);
        //头像点击事件
        account_mainpage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isLoggedIn) {
                    // 如果已登录，则跳转到个人主页
                    ((MainPageActivity) getActivity()).showMyAccountPageFragment();
                } else {
                    // 如果未登录，则跳转到登录界面
                    Intent intent = new Intent(getActivity(), SignInActivity.class);
                    startActivity(intent);
                }
            }
        });

        // 搜索
        EditText editSearchOnMainPage = view.findViewById(R.id.edit_search_on_main_page);
        editSearchOnMainPage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), SearchScreenActivity.class);
                startActivity(intent);
            }
        });

        return view;
    }
}